package com.prs.crm.action.cdxh;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.cdxh.Cdstock;
import com.prs.crm.domain.cdxh.StockPrecautionary;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.warning.Warn;
import com.prs.crm.domain.warning.WarnContent;
import com.prs.crm.service.cdxh.CdstockService;
import com.prs.crm.service.cdxh.StockPrecautionaryService;
import com.prs.crm.service.cdxh.cd_ProductService;
import com.prs.crm.service.oa.NoticeService;
import com.prs.crm.service.warning.TaskRemindService;
import com.prs.crm.service.warning.WarnContentService;
import com.prs.crm.service.warning.WarnService;

public class CdstockAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private StockPrecautionary stockPrecautionary;

	private StockPrecautionaryService stockPrecautionaryService;
    private TaskRemindService taskRemindService;
	private List<StockPrecautionaryService> slist;

	private cd_Product product;
	private Warn warn;
	private WarnService warnService;
	private WarnContent warnContent;
	private WarnContentService warnContentService;

	private cd_ProductService cd_ProductService;
	private Cdstock cdstock;
	private CdstockService cdstockService;
	private PaginationSupport pagination;
	private Integer[] cdstockIds;
	private File upload;
	private String uploadFileName;
	private Person person;
	private Date currentDate;
	private NoticeService noticeService;
	private static int count = 1;

	@Action("editCdstock")
	public String edit() {
		person = AppContext.getAccountContext().currentUser();
		currentDate = new Date();

		return SUCCESS;
	}

	@Action("editCdstock1")
	public String editCdstock1() {

		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveCdstock1", results = {
			@Result(name = "success", type = "redirect", location = "listCdstock"),
			@Result(name = "error", type = "chain", location = "editCdstock1") })
	public String saveCdstock1() {
		Cdstock cdstock1 = this.getCdstockService().getCdstockById(
				cdstock.getId());
		this.getCdstockService().saveOrUpdate(cdstock1);
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "submitCdstock", results = {
			@Result(name = "success", type = "redirect", location = "listCdstock"),
			@Result(name = "error", type = "chain", location = "editCdstock") })
	public String submitCritiqueLiaison() {

		if (cdstock.getMax_amount() == null) {
			addActionError("库存报警下限数");
			return ERROR;
		}
		if (cdstock.getMax_amount() == null) {
			addActionError("库存报警上限数");
			return ERROR;
		}
		this.getCdstockService().submitCdstock(cdstock, upload, uploadFileName);

		this.setPrompt("提交成功");
		return SUCCESS;
	}

	@Action("listProductStock")
	public String listProductStock() {

		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);
		criteria.add(Restrictions.lt("showSafeConfigration", 6));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCdstockService().listByPage(criteria,
				pagination));

		return SUCCESS;
	}

	@Action("listStockNotice")
	public String listStockNotice() {
		// 清理库存没有的预警
//		List<StockPrecautionary> stocks = stockPrecautionaryService.getStockPrecautionaryList();
//		if (stocks != null) {
//			for (int i = 0; i < stocks.size(); i++) {
//				StockPrecautionary s = stocks.get(i);
//				cd_Product cd = null;
//				cd = this.cd_ProductService
//						.getProductObject(s.getProductCode());
//				if (cd == null) {
//					stockPrecautionaryService.setById(s);
//				}
//			}
//		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Warn.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getWarnService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}
	@Action("ListReminds")
	public String ListReminds() {
//        Warn warns=this.getWarnService().
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				WarnContent.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getWarnContentService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}
	@Action("viewStockPrecautionary")
	public String viewStockPrecautionary() {

		stockPrecautionary = this.getStockPrecautionaryService().get(
				stockPrecautionary.getId());
		// System.out.println("bianaho:"+stockPrecautionary.getProductCode());
		product = this.getCd_ProductService().getProductObject(
				stockPrecautionary.getProductCode());
		// System.out.println("product:"+product);
		return SUCCESS;
	}

	@Action(value = "ajaxReadStockNotice", results = { @Result(name = "success", type = "json", params = {
			"root", "prompt" }) })
	public String ajaxReadStockNotice() {
		// int
		// sizes=this.getStockPrecautionaryService().getStockPrecautionaryList().size();
//	   System.out.println("A:");
	   this.getTaskRemindService().getTaskRemind();//任务管理	
//	   System.out.println("B:");
       List<WarnContent> alist=this.getWarnContentService().getWarnContent();
//       System.out.println("alist:"+alist.size());
       List<Warn> blist=this.getWarnService().getWarnings();
//       System.out.println("blist:"+blist.size());
       for(WarnContent b:alist)
       { 
    	Warn warn = new Warn();
 		warn.setWarnstyle(b.getWarnstyl());
 		warn.setWarnstate("1");//显示状态
 		int z=0;;
 		for(Warn bb:blist)
 		{
 			String vb=bb.getWarnstyle();
 			if(vb.equals(b.getWarnstyl()))
 			{
 				z++;
 			} 			
 		}
 		if(z==0)
 		{
 			this.getWarnService().save(warn);
 		}		
       }
		

		int sizes = blist.size();
//		System.out.println("大小:"+sizes);
		if (sizes > 0)
			this.setPrompt("true");
		else
			this.setPrompt("false");
		return SUCCESS;
	}

	@Action("listCdstock")
	public String listCdstock() {

		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Cdstock.class);

		this.setPagination(this.getCdstockService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("listalterCdstock")
	public String listalterCdstock() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Cdstock.class);
		this.setPagination(this.getCdstockService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("listUnCdstock")
	public String listUnCdstock() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Cdstock.class);

		this.setPagination(this.getCdstockService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("viewCdstock")
	public String viewCdstock() {

		return SUCCESS;
	}

	public cd_Product getProduct() {
		return product;
	}

	public void setProduct(cd_Product product) {
		this.product = product;
	}

	public Cdstock getCdstock() {
		return cdstock;
	}

	public void setCdstock(Cdstock cdstock) {
		this.cdstock = cdstock;
	}

	public CdstockService getCdstockService() {
		return cdstockService;
	}

	public void setCdstockService(CdstockService cdstockService) {
		this.cdstockService = cdstockService;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getCdstockIds() {
		return cdstockIds;
	}

	public void setCdstockIds(Integer[] cdstockIds) {
		this.cdstockIds = cdstockIds;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Date getCurrentDate() {
		return currentDate;
	}

	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public static String Enconding(int i) {

		count++;
		String str = new SimpleDateFormat("yyyyMMdd")
				.format(new java.util.Date());
		// System.out.println(str);

		int code1 = Integer.parseInt(str) * 1000;

		// System.out.println(code1);
		int code2 = code1 + i;
		String code = Integer.toString(code2);
		// System.out.println(code);
		count = i + 1;
		return code;
	}

	public static int getCount() {
		return count;
	}

	public static void setCount(int count) {
		CdstockAction.count = count;
	}

	public StockPrecautionary getStockPrecautionary() {
		return stockPrecautionary;
	}

	public void setStockPrecautionary(StockPrecautionary stockPrecautionary) {
		this.stockPrecautionary = stockPrecautionary;
	}

	public StockPrecautionaryService getStockPrecautionaryService() {
		return stockPrecautionaryService;
	}

	public void setStockPrecautionaryService(
			StockPrecautionaryService stockPrecautionaryService) {
		this.stockPrecautionaryService = stockPrecautionaryService;
	}

	public List<StockPrecautionaryService> getSlist() {
		return slist;
	}

	public void setSlist(List<StockPrecautionaryService> slist) {
		this.slist = slist;
	}

	public cd_ProductService getCd_ProductService() {
		return cd_ProductService;
	}

	public void setCd_ProductService(cd_ProductService cd_ProductService) {
		this.cd_ProductService = cd_ProductService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Warn getWarn() {
		return warn;
	}

	public void setWarn(Warn warn) {
		this.warn = warn;
	}

	public WarnService getWarnService() {
		return warnService;
	}

	public void setWarnService(WarnService warnService) {
		this.warnService = warnService;
	}

	public WarnContent getWarnContent() {
		return warnContent;
	}

	public void setWarnContent(WarnContent warnContent) {
		this.warnContent = warnContent;
	}

	public WarnContentService getWarnContentService() {
		return warnContentService;
	}

	public void setWarnContentService(WarnContentService warnContentService) {
		this.warnContentService = warnContentService;
	}

	public TaskRemindService getTaskRemindService() {
		return taskRemindService;
	}

	public void setTaskRemindService(TaskRemindService taskRemindService) {
		this.taskRemindService = taskRemindService;
	}

}
